package com.nongtai.config;

import com.nongtai.interceptor.JwtTokenInterceptor;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 允许前端跨域问题
 * @author yza
 * @version 1.0
 * @date 2024/6/2 12:25
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Resource
    private JwtTokenInterceptor jwtTokenInterceptor;
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**") // 允许所有路径
                .allowedOrigins("http://localhost:5173") // 允许哪些来源
                .allowedMethods("GET", "POST", "PUT", "DELETE") // 允许哪些方法
                .allowedHeaders("*") // 允许哪些头
                .allowCredentials(true); // 是否允许携带凭证（如cookies）
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(jwtTokenInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/login");
    }
}
